package com.mgg.user.service.profile.impl;

import com.mgg.common.util.DateUtils;
import com.mgg.common.dao.DaoSupport;
import com.mgg.user.dto.WxConnectDTO;
import com.mgg.user.entity.BankCardDO;
import com.mgg.user.entity.IdentifyCardDO;
import com.mgg.user.service.profile.ProfileService;
import com.mgg.user.vo.IdentifyCardVO;
import com.mgg.user.vo.ProfileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("profileService")
public class ProfileServiceImpl implements ProfileService {

    @Autowired
    private DaoSupport dao;

    @Override
    public Integer updateProfile(String userId, String username, String headImg, String birthday, String sex, String region) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("username", username);
        map.put("headImg", headImg);
        map.put("birthday", birthday);
        map.put("sex", sex);
        map.put("region",region );
        Integer count = dao.update("ProfileMapper.updateProfile", map);
        return  count;
    }

    @Override
    public ProfileVO getProfileByUserId(String userId) {
        ProfileVO profileVO = dao.findForObject("ProfileMapper.getProfileByUserId", userId);
        IdentifyCardVO identifyCardVO = getUserIdentityCard(userId);
        if (identifyCardVO != null) {
            profileVO.setIdentifyStatus(identifyCardVO.getStatus());
        } else {
            profileVO.setIdentifyStatus(99);
        }
        return profileVO;
    }

    @Override
    public Integer saveUserIdentityCard(String userId, String realName, String identityNumber, String frontImage, String consImage) {
        IdentifyCardDO identifyCardDO = new IdentifyCardDO();
        identifyCardDO.setUserId(userId);
        identifyCardDO.setRealName(realName);
        identifyCardDO.setIdentityNumber(identityNumber);
        identifyCardDO.setFrontImage(frontImage);
        identifyCardDO.setConsImage(consImage);
        identifyCardDO.setStatus("0");
        identifyCardDO.setCreateTime(DateUtils.getTime());
        Integer count = dao.save("ProfileMapper.saveUserIdentityCard",identifyCardDO);
        return count;
    }

    @Override
    public IdentifyCardVO getUserIdentityCard(String userId) {
        IdentifyCardVO identifyCardVO = dao.findForObject("ProfileMapper.getUserIdentityCard", userId);
        return identifyCardVO;
    }

    @Override
    public Integer saveUserBankCard(BankCardDO bankCardDO) {
        Integer count = dao.save("ProfileMapper.saveUserBankCard", bankCardDO);
        return count;
    }

    @Override
    public List<Map<String,Object>> listUserBanlCardByUserId(String userId) {
        List<Map<String,Object>> bankCardVOList = dao.findForList("ProfileMapper.listUserBankCardByUserId", userId);
        return bankCardVOList;
    }

    @Override
    public Integer insertUserConnection(String userId, String providerId, String openid, String unionid, String name, String image) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("providerId", providerId);
        map.put("providerUserId", openid);
        map.put("unionid", unionid);
        map.put("displayName", name);
        map.put("imageUrl", image);
        map.put("rank", 1);
        Integer count = dao.save("ProfileMapper.insertUserConnection", map);
        return count;
    }

    @Override
    public Integer deleteUserConnection(String userId, String providerId) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        paramMap.put("providerId", providerId);
        Integer count = dao.delete("ProfileMapper.deleteUserConnection", paramMap);
        return  count;
    }

    @Override
    public Integer updateUserUnionid(String userId, String providerId, String openid) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        paramMap.put("providerId", providerId);
        paramMap.put("providerUserId", openid);
        Integer count = dao.update("ProfileMapper.updateUserUnionid", paramMap);
        return  count;
    }

}
